﻿using System;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Linq;
 
public class BuildExcelEditor : Editor
{
 
}
 
public class BuildExcelWindow : EditorWindow
{
    private string showNotify;
    private Vector2 scrollPosition = Vector2.zero;
 
    private List<string> fileNameList = new List<string>();
    private List<string> filePathList = new List<string>();
    private List<string> tempFilePathList = new List<string>();


    [MenuItem("ExcelEditor/Excel2ScriptableObject")]
    public static void ShowExcelWindow()
    {
        GetWindow(typeof(BuildExcelWindow));
    }
    
    private void Awake()
    {
        titleContent.text = "Excel Editor";
    }

    private void OnFocus()
    {
        showNotify = "";
        fileNameList.Clear();
        filePathList.Clear();
        GetExcelFile();
    }

    private void OnEnable()
    {
        showNotify = "";
        GetExcelFile();
    }
 
    private void OnDisable()
    {
        showNotify = "";
        fileNameList.Clear();
        filePathList.Clear();
    }
 
    private void OnGUI()
    {
        if (GUILayout.Button("Refresh", GUILayout.Width(200), GUILayout.Height(30)))
        {
            showNotify = "";
            fileNameList.Clear();
            filePathList.Clear();
            GetExcelFile();
        }
        scrollPosition = GUILayout.BeginScrollView(scrollPosition,
            GUILayout.Width(position.width), GUILayout.Height(position.height));
        //创建C#脚本代码
        GUILayout.Space(10);
        GUILayout.Label("Generate C# Script");
        for (int i = 0; i < fileNameList.Count; i++)
        {
            if (GUILayout.Button(fileNameList[i], GUILayout.Width(200), GUILayout.Height(18)))
            {
                SelectExcelToCodeByIndex(i);
            }
        }
        if (GUILayout.Button("Generate All", GUILayout.Width(200), GUILayout.Height(30)))
        {
            SelectExcelToCodeByIndex(-1);
        }
        //创建ScriptableObject asset
        GUILayout.Space(20);
        GUILayout.Label("Generate ScriptableObject");
        for (int i = 0; i < fileNameList.Count; i++)
        {
            if (GUILayout.Button(fileNameList[i], GUILayout.Width(200), GUILayout.Height(18)))
            {
                SelectCodeToAssetByIndex(i);
            }
        }
        if (GUILayout.Button("Generate All", GUILayout.Width(200), GUILayout.Height(30)))
        {
            SelectCodeToAssetByIndex(-1);
        }
        GUILayout.Space(20);
        GUILayout.Label(showNotify);
        GUILayout.EndScrollView();
    }
 
    //读取指定路径下的Excel文件名
    private void GetExcelFile()
    {
        fileNameList.Clear();
        filePathList.Clear();
        tempFilePathList.Clear();
 
        if (!Directory.Exists(ExcelReader.excelFilePath))
        {
            showNotify = "无效路径：" + ExcelReader.excelFilePath;
            return;
        }
        string[] excelFileFullPaths = Directory.GetFiles(ExcelReader.excelFilePath, "*.xlsx");
 
        if (excelFileFullPaths == null || excelFileFullPaths.Length == 0)
        {
            showNotify = "【" + ExcelReader.excelFilePath + "】/n目录未发现Excel文件！";
            return;
        }
 
        tempFilePathList.AddRange(excelFileFullPaths);
        for (int i = 0; i < tempFilePathList.Count; i++)
        {
            string fileName = tempFilePathList[i].Split('/').LastOrDefault();
            fileName = tempFilePathList[i].Split('\\').LastOrDefault();
            //忽略Excel的编辑文件
            if (!fileName.Contains("~$"))
            {
                fileNameList.Add(fileName);
                filePathList.Add(tempFilePathList[i]);
            }
        }
        showNotify = "找到Excel文件：" + fileNameList.Count + "个";
    }
 
    //自动创建C#脚本
    private void SelectExcelToCodeByIndex(int index)
    {
        if (index >= 0 && index < filePathList.Count)
        {
            string fullPath = filePathList[index];
            ExcelReader.ReadOneExcelToCode(fullPath);
        }
        else
        {
            ExcelReader.ReadAllExcelToCode();
        }
    }
 
    //自动创建Asset文件
    private void SelectCodeToAssetByIndex(int index)
    {
        if (index >= 0 && index < filePathList.Count)
        {
            string fullPath = filePathList[index];
            ExcelReader.CreateOneExcelAsset(fullPath);
        }
        else
        {
            ExcelReader.CreateAllExcelAsset();
        }
    }
}